import {
	NetService
} from '../../common/NetService';
import myxz from '../../components/myList.vue';
export default {
	components: {
		myxz
	},
	data() {
		return {
			menu: [],
			menu1: [{
					name: "待处置",
					id: 1,
					check: true,
				},
				{
					name: "待审核",
					id: 2,
					check: false,
				}, {
					name: "未通过 ",
					id: 4,
					check: false,
				},
				{
					name: "已完成",
					id: 3,
					check: false,
				}
			],
			menu2: [{
					name: "已上报",
					id: 1,
					check: true,
				},
				{
					name: "待审核",
					id: 2,
					check: false,
				},
				{
					name: "未通过 ",
					id: 4,
					check: false,
				}, {
					name: "已完成",
					id: 3,
					check: false,
				}
			],
			vModelDatetime: "",
			vModelDatetime2: "",
			scrollTop: 0,
			newDate: new Date().getTime(),
			filters: {
				"startTime": "",
				"endTime": "",
				"status": 1,
				"page": 1,
				"pageSize": 10,
				"uid": "",
				"typeId": 2,
			},
			startTime: "",
			endTime: "",
			list: [],
			itemShow: false,
			item: {},
			cheItem: null,
			title: {},
			selectList: [],
			next: true, //是否下一页
			propType: 0,
			userList: [],

		}
	},
	methods: {
		newnation(obj) {
			console.log(obj, "obj")
			if(obj.myType == 1) {
				this.itemShow = false;
				this.item.actDef = obj.setVal;
				this.item.role = null;
				this.item.user = null;
				this.wxacttopart(obj.setVal.value);
			}
			if(obj.myType == 2) {
				this.itemShow = false;
				this.item.role = obj.setVal;
				this.item.user = null;
				this.wxgetuserbyusername(obj.setVal.value);
			}
			if(obj.myType == 3) {
				this.itemShow = false;
				this.item.user = obj.setVal;
			}
			if(obj.close) {
				this.itemShow = false;
			}
			this.selectList = [];
			console.log(this.item)
		},
		//确认转交
		wxtransit() {
			let _this = this;
			console.log("wxtransit")
			if(!this.item.actDef) {
				uni.showToast({
					title: '请选择工作项',
				});
				return false;
			}
			if(!this.item.role) {
				uni.showToast({
					title: '请选择分配岗位',
				});
				return false;
			}

			let data = {
				recID: this.item.id,
				actDefID: this.item.actDef.value,
				actDefName: this.item.actDef.label,
				roleID: this.item.role.value,
				userID: this.item.user ? this.item.user.value : 0,
			}
			NetService.wxtransit(data)
				.then(res => {
					uni.showToast({
						title: '案件转交成功',
					});
					this.sendMessage(this.item, 1);
					for(let i = 0; i < _this.list.length; i++) {
						let item = _this.list[i];
						console.log("item.id == _this.item.id", item.id == _this.item.id)
						if(item.id == _this.item.id) {
							_this.list.splice(i, 1);
						}
					}
					console.log("_this.list", _this.list)
					this.$refs.popup2.close();
					this.item = {};

				})
				.catch(err => {});
		},
		sendMessage(item, typeId) {
			let openidString = [];
			if(item.user) {
				openidString = [item.user.openid]
			} else {
				this.userList.forEach(ite => {
					openidString.push(ite.openid);
				})
			}
			let data = {
				status: this.filters.typeId,
				typeId: typeId,
				title: item.title,
				caseId: item.id,
				openidString: openidString.join(","),
			}
			NetService.sendMessage(data)
				.then(res => {})
				.catch(err => {});
		},
		toggleTab(t) {
			if(t == 1) {
				this.wxrecact();
			} else if(t == 2) {
				if(this.item && this.item.actDef) {
					this.wxacttopart(this.item.actDef.value)
				}
			} else if(t == 3) {
				if(this.item.role) {
					this.wxgetuserbyusername(this.item.role.value)
				}
			}
		},
		//用户
		wxgetuserbyusername(id) {
			let data = {
				"roleID": id,
			}
			NetService.wxgetuserbyusername(data)
				.then(res => {
					this.userList = res;
					this.selectList = [];
					res.forEach(item => {
						let obj = {
							"label": item.userName,
							"openid": item.openid,
							"value": item.userID,
						}
						this.selectList.push(obj);
					})
					this.cheItem = this.item.user;
					this.item.myType = 3;
					this.itemShow = true;
				})
				.catch(err => {});
		},
		//获取岗位信息成功
		wxacttopart(id) {
			this.selectList = [];
			NetService.wxacttopart(id)
				.then(res => {
					this.selectList = [];
					res.forEach(item => {
						let obj = {
							"label": item.roleName,
							"value": item.roleID,
						}
						this.selectList.push(obj);
					})
					this.cheItem = this.item.role;
					this.item.myType = 2; //1工作项
					this.itemShow = true;
				})
				.catch(err => {});
		},
		//获取工作项列表成功
		wxrecact() {
			this.selectList = [];
			NetService.wxrecact(this.item.id)
				.then(res => {
					this.selectList = [];
					res.forEach(item => {
						let obj = {
							"label": item.actDefName,
							"value": item.actDefID,
						}
						this.selectList.push(obj);
					})
					console.log(" this.selectList", this.selectList)
					this.cheItem = this.item && this.item.actDef;
					this.item.myType = 1; //1工作项
					this.itemShow = true;
				})
				.catch(err => {});
		},
		popupConfirm() {
			let user = uni.getStorageSync('user');
			let data = {
				recID: this.item.id,
				userID: user.userID,
			}
			NetService.wxassigntask(data)
				.then(res => {
					if(this.propType == 1) {
						this.toDetail(this.item, this.propType)
					} else {
						this.showNext();
					}

				})
				.catch(err => {});
		},
		openProp(obj, type = 0) {
			this.item = obj;
			this.propType = type
			if(obj.mid) {
				if(type == 1) {
					//处置
					this.toDetail(obj, type)
				} else {
					this.showNext();

				}

			} else {
				this.$refs.popup.open()
			}
		},
		//显示转交
		showNext() {
			this.$refs.popup2.open()
		},
		open() {
			this.$refs.calendar.open();
		},
		confirm(e) {
			console.log(e)
			if(e.range) {
				console.log(Date.parse(e.range.before), Date.parse(e.range.after))
				if(Date.parse(e.range.before) < Date.parse(e.range.after)) {
					this.startTime = e.range.before;
					this.endTime = e.range.after;
				} else {
					this.startTime = e.range.after;
					this.endTime = e.range.before;
				}

				this.filters.page = 1;
				this.init();
			}
		},
		upper() {},

		scroll(e) {
			this.scrollTop = e.detail.scrollTop
		},
		lower() {
			//上拉加载
			console.log(this.next)
			if(this.next) {
				this.filters.page++;
				this.init();
			}
		},
		init() {
			let u = uni.getStorageSync('user');
			if(u) {
				this.filters.uid = u.userID;
			}
			if(this.startTime) {
				this.filters.startTime = new Date(this.startTime).getTime();
				//this.filters.startTime = this.startTime;
			}
			if(this.endTime) {
				this.filters.endTime = new Date(this.endTime).getTime();
				//this.filters.endTime = this.endTime;

			}
			NetService.wxgetreclist(this.filters)
				.then(res => {
					let list = [];
					if(this.filters.page == 1) {
						list = res;
					} else {
						list = [...this.list, ...res]
					}
					this.list = list;
					this.next = res.length >= this.filters.pageSize ? true : false;
				})
				.catch(err => {});
		},
		changeState(obj) {
			console.log("1", obj);
			this.filters.page = 1;
			this.menu.forEach(item => {
				if(item.id == obj.id) {
					item.check = true;
					this.filters.status = item.id
				} else {
					item.check = false;
				}
			})
			this.init();

		},
		toDetail(item, t) {
			uni.navigateTo({
				url: '/pages/caseDetail/caseDetail?id=' + item.id + "&type=" + t
			});
		},
	},
	onLoad(option) {
		let _this = this;
		if(option.type) {
			this.filters.typeId = parseInt(option.type);
			this.menu = this['menu' + this.filters.typeId];
		}

	},
	onShow() {
		let _this = this;
		this.$getOpenId(() => {
			let u = uni.getStorageSync('user');
			if(!u.userID) {
				uni.showToast({
					title: "您未登陆绑定，请登陆"
				});
				setTimeout(function() {
					uni.navigateTo({
						url: "/pages/bind/bind"
					});
				}, 1000);
				return false;
			}
			_this.init();
			//#ifdef H5
			// 判断是否是微信内
			let en = window.navigator.userAgent.toLowerCase();
			// 匹配en中是否含有MicroMessenger字符串
			if(en.match(/MicroMessenger/i) == 'micromessenger') {
				_this.$share({
					title: "渝中网格化系统-案件列表"
				});
			}
			//#endif
		});
	},

}